package com.hula.common;

import org.apache.http.conn.HttpClientConnectionManager;

import java.util.concurrent.TimeUnit;

/**
 * @author liuhe
 * @date 2021/3/25
 * @Description 释放httpclient连接
 */
public class ReleaseConnectionThread extends Thread{
    private HttpClientConnectionManager connMgr;
    private volatile boolean shutdown = false;

    public ReleaseConnectionThread(HttpClientConnectionManager connMgr) {
        this.connMgr = connMgr;
    }

    @Override
    public void run() {
        try {
            while (!shutdown) {
                synchronized (this) {
                    // 等待2秒
                    wait(2000);
                    // 关闭失效连接
                    connMgr.closeExpiredConnections();
                    //关闭空闲超过5秒的连接
                    connMgr.closeIdleConnections(5000, TimeUnit.MILLISECONDS);
                }
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
